/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ScrollPane.java
 *
 * Created on 24-jul-2010, 21:37:13
 */

package facturasapp;

import facturasapp.data.DataManager;
import facturasapp.events.FacturaAdapter;
import facturasapp.events.FacturaEvent;
import facturasapp.events.EventManager;
import java.awt.GridLayout;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneLayout;
import storeDataModel.Invoice;
import storeDataModel.ProductRelation;

/**
 *
 * @author CiberXtrem
 */
public class ShoppingCartPane extends javax.swing.JScrollPane {

    /** Creates new form ScrollPane */
    public ShoppingCartPane() {
        //initComponents();
        initCustomComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setName("Form"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    public void initCustomComponents(){
        
        this.setLayout(new ScrollPaneLayout());
        //this.setBounds(new Rectangle(0, 0, 400, 100));
        this.setPreferredSize(new java.awt.Dimension(200, 382));
        //this.setMinimumSize((new java.awt.Dimension(200, 400)));
        this.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        this.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        //this.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0)); //don't
        //this.setBorder(null);

        GridLayout gridLayout = new GridLayout(99,0);
        panel.setLayout(gridLayout);

        this.getViewport().add(panel);
       

        //AddListener//
        EventManager.getInstance().addFacturaListener(new FacturaAdapter() {

            public void productAdded(FacturaEvent e) {
                //DataValues
                invoice = (Invoice) DataManager.getInstance().getDataObject("currentInvoice");

                //Obtenemos el producto desde source del evento
                ProductRelation productRelation = (ProductRelation) e.getSource();

                //Agregamos el productRelation a Invoice
                invoice.addProductRelation(productRelation);

                //agregamos a la lista de la compra visual la nueva ProductRelation y le pasamos el productRelation data para que lo pueda modificar
                ProductShoppingCartPane productShoppingCartPane = new ProductShoppingCartPane(productRelation);
                //Setting values to produdShopPane
                productShoppingCartPane.codigoField.setText(productRelation.getProducto().getCodigo());
                productShoppingCartPane.descripcionField.setText(productRelation.getDescripcion());
                productShoppingCartPane.precioField.setText(String.valueOf(productRelation.getPrecio()));
                
                productShoppingCartPane.importeField.setText(String.valueOf(productRelation.getImporte()));
                productShoppingCartPane.cantidadField.setText(String.valueOf(productRelation.getCantidad()));

                //Lanzamos evento para indicar que la factura se ha actualizado
                FacturaEvent ev = new FacturaEvent(this); //NO necesitamos este objeto en este caso
                EventManager.getInstance().fireFacturaEvent(ev, EventManager.INVOICE_UPDATED);

                panel.add(productShoppingCartPane);
                panel.revalidate();
                panel.repaint();
                
            }

            public void productRemoved(FacturaEvent e) {
                //Lanzamos evento para indicar que la factura se ha actualizado
                EventManager.getInstance().fireFacturaEvent(e, EventManager.INVOICE_UPDATED);

                ProductShoppingCartPane productShoppingCartPane = (ProductShoppingCartPane) e.getSource();
                panel.remove(productShoppingCartPane);
                panel.revalidate();
                panel.repaint();
            }

            public void newInvoice(FacturaEvent e) {
                panel.removeAll();
                panel.revalidate();
                panel.repaint();
            }
        });
        //End AddListener//


    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

    //Custom Variables
    JPanel panel = new JPanel();

    Invoice invoice = null;
    //End Custom Variables
}
